#!/usr/sbin/rsct/perl5/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)29   1.13   src/rsct/rm/SensorRM/cli/bin/mksensor.perl, sensorcli, rsct_rzauh, rzauh0431a 1/20/04 13:10:21

# Creates an RMC sensor

use strict;
use locale;
BEGIN
  {
	# this enables us to redirect where it looks for other RSCT files during development
	$::rsctroot = $ENV{'RSCT_ROOT'} || '/usr/sbin/rsct';
	$::rsctpm = "$::rsctroot/pm";
	$::rsctmsgmaps = "$::rsctroot/msgmaps";
  }

use lib $::rsctpm;
use Getopt::Std;
use CT_cli_utils qw(printIMsg printEMsg);
use Socket;

$main::PROGNAME = 'mksensor';
$main::MSGCAT = 'sensorcli.cat';
$main::LSMSG = '/usr/sbin/rsct/bin/ctdspmsg';

# For the usage, see sensorcli.msg
sub usage { printIMsg('IMsgMksensorUsage_new');  exit (scalar(@_) ? $_[0] : 1); }

# Parse the cmd line args and check them
if (! getopts('i:n:e:hvV') ) { &usage; }
if (scalar(@ARGV) < 2 || $::opt_h) { &usage; }
if ($::opt_V) { $::opt_v = 1; }

my $name = shift @ARGV;
my $cmd = join(' ', @ARGV);
# we use whoami instead of $USER or $LOGNAME so it will work with su
my $username = `whoami`;    chop $username;
if (!defined($::opt_i)) { $::opt_i = 60; }

if (defined($::opt_n))
  {
	#todo: support symbolic names for values 2 and 3
	if ($ENV{CT_MANAGEMENT_SCOPE}!=2 && $ENV{CT_MANAGEMENT_SCOPE}!=3) { $ENV{CT_MANAGEMENT_SCOPE}=4; }
	# Only 1 node is allowed with mksensor
	#&resolve(\$::opt_n);
	$::opt_n = "::NodeNameList::{'$::opt_n'}";
  }
else { $ENV{CT_MANAGEMENT_SCOPE}=1; }

if (defined($::opt_e))
   {
    $::opt_e = "::ErrorExitValue::$::opt_e";
   }

# Deal with quotes in the cmd
# if the value already has quotes around it, do not quote it
if (!( $cmd =~ /^(".*"|'.*')$/ ))
  {
	# if the value has imbedded double quotes, use single quotes
	if ($cmd =~ /\"/) { $cmd = qq('$cmd'); } # use the other quotes
	else 		# use double quotes
	  {
		$cmd =~ s/\"/\\\"/sg;     # escape embedded double quotes
		$cmd = qq("$cmd");
	  }
  }


my $cmd = qq(/usr/bin/mkrsrc-api IBM.Sensor::Name::${name}::Command::${cmd}::UserName::${username}::RefreshInterval::$::opt_i$::opt_n$::opt_e 2>&1);
if ($::opt_v) { $main::PROGNAME = 'lssensor'; printIMsg('IMsgRmcCmd', $cmd); $main::PROGNAME = 'mksensor'; }

my @output = `$cmd`;
my $rc = $? >> 8;
if ($rc)
  {
	if (!$::opt_v) { $output[0] =~ s/.*::.*::.*::.*::.*:://; }
	print @output;
	exit $rc;
  }

exit $rc;


# Not used right now
sub resolve
  {
	my ($hostref) = @_;
	if ($$hostref =~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) # an ip address
	  {
		my $packedaddr = inet_aton($$hostref);
		my $hostname = gethostbyaddr($packedaddr, AF_INET);
		if (length($hostname)) { $$hostref = $hostname; }
	  }
	else						# they specified a hostname, but it may be a short name
	  {
		my ($hostname, $aliases, $addrtype, $length, @addrs) = gethostbyname($$hostref);
		if (length($hostname)) { $$hostref = $hostname; }
	  }
  }
